<template>
  <el-form ref="form" :rules="rules" :model="form" label-width="100px">
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="form.name" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input v-model="form.code" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" placeholder="请选择部门负责人">
        <el-option v-for="item in peoples" :key="item.id" :label="item.username" :value="item.username" />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model="form.introduce" type="textarea" placeholder="1-300个字符" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">确定</el-button>
      <el-button @click="hCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { getEmployeeSimple } from '@/api/employees'
import { addDepartments, getDepartDetail, updateDepartments } from '@/api/departments'
export default {
  props: {
    existedNameList: { // name
      type: Array,
      default() {
        return []
      }
    },
    existedCodeList: { // code
      type: Array,
      required: true
    },
    pid: { // 传pid
      type: String,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    }
  },
  data() {
    const validCode = (rule, value, callback) => {
      const list = this.isEdit ? this.existedCodeList.filter(item => item !== this._code) : this.existedCodeList
      list.includes(value) ? callback(`部门编码 ${value}  重复了`) : callback()
      console.log(list, value)
    }
    const validName = (rule, value, callback) => {
      const list = this.isEdit ? this.existedNameList.filter(item => item !== this._name) : this.existedNameList
      console.log(list, value)
      list.includes(value) ? callback(`部门名字 ${value}  已被占用`) : callback()
    }
    return {
      peoples: [], // 获取部门负责人信息

      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },

      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: ['blur', 'change'] },
          { validator: validName, trigger: ['blur', 'change'] }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: ['blur', 'change'] },
          {
            validator: validCode, trigger: ['blur', 'change']
          }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  async created() {
    this.loadEmployee()
    // 如果是编辑,应该要去获取详情
    if (this.isEdit) {
      // 获取详情
      const res = await getDepartDetail(this.pid)
      console.log('获取详情是', res)
      this.form = res.data
      this._code = this.form.code
      this._name = this.form.name
    }
  },
  methods: {
    async loadEmployee() {
      const rs = await getEmployeeSimple()
      console.log('员工信息', rs)
      this.peoples = rs.data
    },
    async doAdd() {
      // 去获取pid： 当前要添加的部门所在的父级的id
      // 从props中获取
      // 1. 组装参数
    //   const params =
    //   console.log(params)
      // 2. 调用接口
      const rs = await addDepartments({ ...this.form, pid: this.pid })
      console.log(rs)
      console.log(this.pid)

      // 3. 通知父组件：关闭弹层，再次更新数据
      this.$emit('update-depart')
    },
    // 执行具体的编辑动作
    async doEdit() {
      // 1 调用接口 , 这里的this.form中已经有所有要传递的参数了
      const res = await updateDepartments(this.form)
      console.log('编辑操作的返回结果是', res)
      // 2 . 通知父组件 : 关闭弹层 , 再次更新数据
      this.$emit('update-depart')
    },
    // 用户点击了取消
    hCancel() {
      // 通知父组件去关闭弹层
      this.$emit('close')
    },
    // 表单验证
    onSubmit() {
      // 表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          console.log('submit!')
          if (this.isEdit) {
            // 做编辑
            this.doEdit()
          } else {
            // 做添加
            this.doAdd()
          }
          // 通过
        } else {
          alert('error')
        }
      })
    }
  }
}
</script>

<style>

</style>
